package com.java.controller.api;


import com.alibaba.fastjson.JSON;
import com.java.hfb.RequestHelper;
import com.java.instans.MqConst;
import com.java.result.Result;
import com.java.service.RabbitService;
import com.java.service.UserAccountService;
import com.java.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.ReactiveSubscription;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 * <p>
 * 用户账户 前端控制器
 * </p>
 *
 * @author Helen
 * @since 2021-07-08
 */

@Api("用户账户")
@RestController
@RequestMapping("/api/core/userAccount")
public class UserAccountController {

    private static final Logger log = LoggerFactory.getLogger(UserAccountController.class);

    @Resource
    private UserAccountService accountService;



    @ApiOperation("充值")
    @PostMapping("auth/commitCharge/{chargeAmt}")
    public Result commitCharge(@PathVariable BigDecimal chargeAmt, HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        String formStr = accountService.commitCharge(chargeAmt,userId);
        return Result.ok(formStr);
    }

    //充值同步账户
    @PostMapping("/notify")
    public String notify(HttpServletRequest request){

        Map<String, Object> map = RequestHelper.switchMap(request.getParameterMap());
        //签名校验
        if(RequestHelper.isSignEquals(map)){
            //判断充值是否成功
            if("0001".equals(map.get("resultCode"))){
                //同步
                accountService.Commitnotify(map);
            }else{
                //不管充值失败还是网络原因都返回success
                return "success";
            }
        }
        return "fail";
    }


    @ApiOperation("账户余额")
    @GetMapping("auth/getAmount")
    public Result getAmount(HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        BigDecimal amount = accountService.getAmount(userId);
        return Result.ok(amount);
    }

    @ApiOperation("提现")
    @PostMapping("auth/withdraw/{chargeAmt}")
    public Result withdraw(@PathVariable BigDecimal chargeAmt, HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        String formStr = accountService.withdrasCommit(chargeAmt,userId);
        return Result.ok(formStr);
    }

    //提现同步账户
    @PostMapping("/notifyWithdraw")
    public String notifyWithdraw(HttpServletRequest request){

        Map<String, Object> map = RequestHelper.switchMap(request.getParameterMap());
        //签名校验
        if(RequestHelper.isSignEquals(map)){
            //判断充值是否成功
            if("0001".equals(map.get("resultCode"))){
                //同步
                accountService.notifyWithdraw(map);
            }else{
                //不管充值失败还是网络原因都返回success
                return "success";
            }
        }
        return "fail";
    }

}

